package cn.yangliu.shiro.redis;

import cn.yangliu.shiro.ShiroConst;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Set;

public class RedisManager extends org.crazycake.shiro.RedisManager {

    private static JedisPool jedisPool = null;

    private String host;

    private int port;

    private int timeout;

    private String password;

    private int database;

    @Override
    public String getHost() {
        return host;
    }

    @Override
    public void setHost(String host) {
        this.host = host;
    }

    @Override
    public int getPort() {
        return port;
    }

    @Override
    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public int getTimeout() {
        return timeout;
    }

    @Override
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public void setPassword(String password) {
        this.password = password;
    }

    public int getDatabase() {
        return database;
    }

    public void setDatabase(int database) {
        this.database = database;
    }

    public void init(){
        jedisPool = new JedisPool(new JedisPoolConfig(),host,
                port, timeout,
               password, database, null);
    }

    public byte[] set(byte[] key, byte[] value) {
        Jedis jedis =  jedisPool.getResource();

        try {
            jedis.set(key, value);
            if (ShiroConst.EXPIRE != 0) {
                jedis.expire(key, ShiroConst.EXPIRE);
            }
        } finally {
            jedisPool.returnResourceObject(jedis);
        }

        return value;
    }

    public byte[] get(byte[] key) {
        Jedis jedis =  jedisPool.getResource();

        byte[] value1;
        try {
            value1 = jedis.get(key);
        } finally {
            jedisPool.returnResourceObject(jedis);
        }

        return value1;
    }



    public byte[] set(byte[] key, byte[] value, int expire) {
        Jedis jedis = jedisPool.getResource();

        try {
            jedis.set(key, value);
            if (expire != 0) {
                jedis.expire(key, expire);
            }
        } finally {
            jedisPool.returnResourceObject(jedis);
        }

        return value;
    }

    public void del(byte[] key) {
        Jedis jedis = jedisPool.getResource();

        try {
            jedis.del(key);
        } finally {
            jedisPool.returnResourceObject(jedis);
        }

    }

    public void flushDB() {
        Jedis jedis = jedisPool.getResource();

        try {
            jedis.flushDB();
        } finally {
            jedisPool.returnResourceObject(jedis);
        }

    }

    public Long dbSize() {
        Long dbSize = Long.valueOf(0L);
        Jedis jedis =  jedisPool.getResource();

        try {
            dbSize = jedis.dbSize();
        } finally {
            jedisPool.returnResourceObject(jedis);
        }

        return dbSize;
    }

    public Set<byte[]> keys(String pattern) {
        Set keys ;
        Jedis jedis =  jedisPool.getResource();

        try {
            keys = jedis.keys(pattern.getBytes());
        } finally {
            jedisPool.returnResourceObject(jedis);
        }

        return keys;
    }
}
